package com.zmn.oms.services.impl.visit;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.model.entity.visit.OrderVisit;
import com.zmn.oms.model.entity.visit.OrderVisitQuery;
import com.zmn.oms.persistence.interfaces.visit.OrderVisitDao;
import com.zmn.oms.services.interfaces.visit.OrderVisitService;
import io.shardingsphere.api.HintManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：
 *
 * @author yule
 * @date 2021/3/10 19:00
 */
@Service
public class OrderVisitServiceImpl implements OrderVisitService {

    @Resource
    private OrderVisitDao orderVisitDao;

    @Override
    public OrderVisit findOrderVisitByQuery(OrderVisitQuery query) {
        OrderVisit orderVisit = orderVisitDao.findOrderVisitByQuery(query);
        if (Objects.isNull(orderVisit)) {
            try (HintManager hintManager = HintManager.getInstance()) {
                hintManager.setMasterRouteOnly();
                return orderVisitDao.findOrderVisitByQuery(query);
            }
        }
        return orderVisit;
    }

    @Override
    public OrderVisit findOrderVisitByQuerySrcMaster(OrderVisitQuery query) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderVisitDao.findOrderVisitByQuery(query);
        }
    }

    @Override
    public List<OrderVisit> listByOrderId(Long orderId) {
        return orderVisitDao.listByOrderId(orderId);
    }

    @Override
    public List<OrderVisit> listByOrderIdSrcMaster(Long orderId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderVisitDao.listByOrderId(orderId);
        }
    }

    @Override
    public List<OrderVisit> listOrderVisitByOrderIdList(List<Long> orderIdList) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderVisitDao.listOrderVisitByOrderIdList(orderIdList);
        }
    }

    /**
     * 按工单id和师傅id查询
     *
     * @param orderId
     * @return
     */
    @Override
    public List<OrderVisit> listByOrderIdAndMasterId(Long orderId, Integer masterId) {
        return orderVisitDao.listByOrderIdAndMasterId(orderId, masterId);
    }

    /**
     * 按工单id和师傅id查询（主库）
     *
     * @param orderId
     * @return
     */
    @Override
    public List<OrderVisit> listByOrderIdAndMasterIdSrcMaster(Long orderId, Integer masterId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            return orderVisitDao.listByOrderIdAndMasterId(orderId, masterId);
        }
    }

    @Override
    public int insertOrderVisit(OrderVisit orderVisit) {
        return orderVisitDao.insert(orderVisit);
    }

    @Override
    public int updateByKey(OrderVisit orderVisit) {
        orderVisit.setUpdateTime(DateUtil.getNow());
        return orderVisitDao.updateByKey(orderVisit);
    }

    @Override
    public int updatePayStatusByKey(OrderVisit orderVisit) {
        orderVisit.setUpdateTime(DateUtil.getNow());
        return orderVisitDao.updatePayStatusByKey(orderVisit);
    }
}
